<?php

namespace App\Admin\Controllers\Virtual;

use App\Admin\Repositories\Virtual\Virtual;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;
use App\Models\Virtual\Virtual as VirtualModel;

class VirtualController extends AdminController
{
    protected $title = '情境学习';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new Virtual(['category', 'virtual_word']), function (Grid $grid) {
            $grid->column('id')->sortable();
            $grid->column('unit_name', '单元名称');
            $grid->column('page_number', '页码');
            $grid->column('title', '书页标题');
            $grid->column('image', '封面图');
            $grid->column('video', '视频');
            $grid->column('virtual_word', '卡点')
                ->display(function ($value) {
                    return count($value);
                });
            $grid->column('is_read', '是否跟读')->display(function ($is_read) {
                return VirtualModel::$isShowMap[$is_read];
            });
            $grid->column('updated_at', '更新时间')->sortable();

            $grid->selector(function (Grid\Tools\Selector $selector) {
                $selector->select('category.id', '年级', Virtual::categoryList());
                $selector->select('is_read', '类型', VirtualModel::$isShowMap);
            });

            $grid->filter(function (Grid\Filter $filter) {
                $filter->panel();
                $filter->equal('id')->width(2);
            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new Virtual(), function (Show $show) {
            $show->field('id');
            $show->field('category_id');
            $show->field('unit_name');
            $show->field('page_number');
            $show->field('title');
            $show->field('image');
            $show->field('video');
            $show->field('is_read');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new Virtual(), function (Form $form) {
            $form->display('id');
            $form->text('category_id');
            $form->text('unit_name');
            $form->text('page_number');
            $form->text('title');
            $form->text('image');
            $form->text('video');
            $form->text('is_read');

            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
